package commands;

import java.util.ArrayList;
import java.util.Date;

import logic.DeadlineParser;

import storage.*;

public class Search {
	public static ArrayList<Task> searchResultList;
	private static ArrayList<Task> displayedTasks_;

	public String listTasks() {
		displayedTasks_ = TaskMemoryController.getTasks();
		return "";
	}

	public ArrayList<Task> getDisplayedTaskList() {
		return displayedTasks_;
	}

	public static ArrayList<Task> getIncompleteTasksInNextDays(int nextDays) {
		displayedTasks_ = new ArrayList<Task>();
		for (int i = 1; i <= nextDays; i++) {
			displayedTasks_.addAll(getIncompleteTasksOn(i));
		}
		return displayedTasks_;
	}

	public String searchByStringDate(String keywords, String date) {
		DeadlineParser dp = new DeadlineParser();
		ArrayList<Task> full_list = TaskMemoryController.getTasks();
		String date_feedback = "";
		if (date != "") {
			try {
				date = date.replaceAll("\\s+", " ");
				date = date.toLowerCase();
				if (date.contains(" to")) {
					if (date.contains("from")) {
						Date from = dp.parseString(date.split("from")[1].split(" to")[0]
								.trim());
						Date to = dp.parseString(date.split(" to")[1].trim());
						full_list = TaskMemoryController.filterByDateRange(full_list, from,
								to, 0);
						date_feedback = " from " + from + " to " + to;
					} else {
						Date from = dp.parseString(date.split(" to")[0].trim());
						Date to = dp.parseString(date.split(" to")[1].trim());
						full_list = TaskMemoryController.filterByDateRange(full_list, from,
								to, 0);
						date_feedback = " from " + from + " to " + to;
					}
				} else if (date.contains("from")) {
					Date from = dp.parseString(date.split("from")[1].trim());
					full_list = TaskMemoryController.filterByDateRange(full_list, from,
							null, 0);
					date_feedback = " from " + from + " onwards";
				} else if (date.contains("before")) {
					Date to = dp.parseString(date.split("before")[1].trim());
					full_list = TaskMemoryController.filterByDateRange(full_list, null,
							to, 0);
					date_feedback = " before " + to;
				} else if (date.contains("on")) {
					Date on = dp.parseString(date.split("on")[1].trim());
					Date end = new Date();
					end.setTime(on.getTime() + (3600 * 24));
					full_list = TaskMemoryController.filterByDateRange(full_list, on,
							end, 0);
					date_feedback = " on " + on;
				}
			} catch (Exception e) {
				date = "";
				date_feedback = ". Invalid date string.";
			}
		}
		if (full_list == null && date != "") {
			full_list = TaskMemoryController.getTasks();
			date_feedback = ". No entries" + date_feedback;
		}
		ArrayList<Task> exact_search = TaskMemoryController.searchByTitleExact(
				full_list, keywords);
		ArrayList<Task> fuzzy_search = TaskMemoryController.searchByTitleFuzzy(
				full_list, keywords, 0.2);
		ArrayList<Task> results = TaskMemoryController.unionTaskLists(exact_search,
				fuzzy_search);
		searchResultList = results;
		displayedTasks_ = results;
		return "<Searchy> Search for: " + keywords + date_feedback;
	}

	public static ArrayList<Task> getIncompleteTasksBeforeNextDays(int nextDays) {
		// reset displayTaskList
		displayedTasks_ = new ArrayList<Task>();
		// add from 31 days ago (day -30)to today (day 1) to nth day (day n)
		// in order to display task according to date (sort by date)
		for (int i = -30; i <= nextDays; i++) {
			displayedTasks_.addAll(getIncompleteTasksOn(i));
		}
		return displayedTasks_;
	}

	private static ArrayList<Task> getIncompleteTasksOn(int nthDay) {
		// today: nthDay = 1
		// tomorrow: nthDay = 2
		// 3rd day: nthDay =3
		Date today = new Date();
		Date thisDay = new Date(today.getTime() + (nthDay - 1) * 24 * 60 * 60
				* 1000);
		Date endOfThisDay = new Date(thisDay.getTime() + 24 * 60 * 60 * 1000);
		ArrayList<Task> incompleteTasks = new ArrayList<Task>();
		incompleteTasks = TaskMemoryController.filterByStatus(
				TaskMemoryController.getTasks(), TaskStatus.INCOMPLETE, 0);
		return TaskMemoryController.filterByDateRange(incompleteTasks, thisDay,
				endOfThisDay, 0);
	}

	public static Date lastDay(ArrayList<Task> tasks) {
		Date lastDay = new Date();
		lastDay = tasks.get(0).getDeadline();
		for (Task task : tasks) {
			if (task.getDeadline().after(lastDay)) {
				lastDay = task.getDeadline();
			}
		}
		return lastDay;
	}

	public static Date firstDay(ArrayList<Task> tasks) {
		Date firstDay = new Date();
		firstDay = tasks.get(0).getDeadline();
		for (Task task : tasks) {
			if (task.getDeadline().before(firstDay)) {
				firstDay = task.getDeadline();
			}
		}
		return firstDay;
	}

}
